package com.vnp.cooking;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.vnp.cooking.adapter.NhabepGridAvapter;
import com.vnp.cooking.adapter.NhabepListAdpater;
import com.vnp.cooking.bean.ObjCookpad;
import com.vnp.cooking.httpclient.HttpClientHelper;
import com.vnp.cooking.httpclient.MyJsonHttpResponseHandler;
import com.vnp.cooking.utils.PreferenceHelper;
import com.vnp.cooking.utils.Utils;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class DetailBoSuuTapActivity extends Activity implements OnClickListener{
	
	ListView listBst;
	GridView gridBst;
	ImageButton btnBack;
	NhabepListAdpater listAdapter;
	NhabepGridAvapter gridAdapter;
	ImageButton btnSortDisplay;
	
	
	// load more
	boolean isLoading = false;
	Button btnLoadmore;
	int currentPage = 1;
	
	ArrayList<ObjCookpad> objCookpads = new ArrayList<ObjCookpad>();
	
	// footer view
	View footerMain;
	Button btnLoadMore;
	ProgressBar progressBarLoadmore;
	PreferenceHelper preferenceHelper;
	ProgressBar progressBar;
	
	String collectionId;
	String nameBST;
	// title bst 
	TextView titleName;
		
	View actionbar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_bo_suu_tap);
		
		collectionId = getIntent().getStringExtra("collection_id");
		nameBST = getIntent().getStringExtra("nameBST");
		
		
		titleName = (TextView) findViewById(R.id.title_ten_bst);
		
		listBst = (ListView) findViewById(R.id.list_detail_bst);
		gridBst = (GridView) findViewById(R.id.gridview);
		actionbar = (View) findViewById(R.id.actionbar);
		
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnSortDisplay = (ImageButton) findViewById(R.id.btn_sort_display);
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		// add footer listview
//		addFooterView();
		getDetailBST(collectionId);
		gridAdapter = new NhabepGridAvapter(DetailBoSuuTapActivity.this, objCookpads);
		listAdapter = new NhabepListAdpater(DetailBoSuuTapActivity.this, objCookpads);
		listBst.setAdapter(listAdapter);
		
		setFont();
		
		init();
		
	}
	
	private void setFont() {
		titleName.setTypeface(Utils.getTypefaceRegular(DetailBoSuuTapActivity.this));
	}
	
	
	void init() {
		titleName.setText(nameBST);
		btnSortDisplay.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		actionbar.setOnClickListener(this);
	}
	
	void getDetailBST(String collectionID) {
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onFailure(Throwable error) {
				hiddenProgessDialog();
				super.onFailure(error);
			}

			@Override
			public void onSuccess(JSONObject re) {
				hiddenProgessDialog();
				try {
					int status = re.getInt("status");
					switch (status) {
					case 200:
						JSONArray data = re.getJSONArray("data");
						Log.i("DATA","BO SUU TAP: "+data.toString());
						for (int i = 0; i < data.length(); i++) {
							JSONObject jo = data.getJSONObject(i);
							ObjCookpad objCookpad = new ObjCookpad();
							objCookpad.setId(jo.getString("id"));
							objCookpad.setNameCookpad(jo.getString("name"));
							// getImage
							JSONObject image = jo.getJSONObject("image");
							objCookpad.setUrlImageCookpad(image.getString("cover"));
							
							objCookpad.setDescriptionCookpad(jo.getString("desc"));
							objCookpad.setViewedCookpad(jo.getInt("view_count"));
							objCookpad.setLikeCookpad(jo.getInt("like_count"));
							objCookpad.setThoigianNauan(jo.getString("time"));
							
							JSONObject author = jo.getJSONObject("author");
							objCookpad.setNamePersonCookpad(author.getString("name"));
							objCookpad.setIdNamePerson(author.getString("id"));
							
							objCookpads.add(objCookpad);
							listAdapter.notifyDataSetChanged();
						}
						
//						footerMain.setVisibility(View.VISIBLE);
						break;
					case 400:
						break;
					case 404: 
						break;

					default:
						break;
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			
		});
		
		httpClientHelper.getDetailBST(collectionID);
	}

	

	// add footer view cong thuc da dang
	void addFooterView() {
		View loadMoreView = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.footer_loadmore, null, false);
		footerMain = (View) loadMoreView.findViewById(R.id.footer_main);
		footerMain.setVisibility(View.GONE);
		listBst.addFooterView(loadMoreView);
		btnLoadMore = (Button) loadMoreView.findViewById(R.id.btn_load_more);
		progressBarLoadmore = (ProgressBar) loadMoreView.findViewById(R.id.progressBar1);
		btnLoadMore.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		
		case R.id.actionbar:
			listBst.smoothScrollToPosition(0);
			break;
		
		case R.id.btn_back:
			closeActivity();
			break;

		case R.id.btn_sort_display:
			changeListGrid();
			break;
		
		case R.id.btn_load_more:
			
			break;
		}
	}
	
	// change list and grid
	public void changeListGrid() {
		if (gridBst.getVisibility() == View.VISIBLE) {
			gridBst.setVisibility(View.GONE);
			listBst.setVisibility(View.VISIBLE);
			listBst.setAdapter(listAdapter);
			btnSortDisplay.setImageResource(R.drawable.ic_grid_sort_03);
		} else {
			listBst.setVisibility(View.GONE);
			gridBst.setVisibility(View.VISIBLE);
			gridAdapter = new NhabepGridAvapter(this,
					objCookpads);
			gridBst.setAdapter(gridAdapter);
			btnSortDisplay.setImageResource(R.drawable.ic_list_sort_05);
		}
	}
	
	// hide progress bar
	void hiddenProgessDialog() {
		try {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					try {
						progressBar.setVisibility(View.GONE);
					} catch (Exception e) {
					}
				}
			});
		} catch (Exception e) {
		}
	}
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.open_main, R.anim.close_next);
	}


	@Override
	public void onBackPressed() {
		closeActivity();
	}
}
